package boyan.douban;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import boyan.model.BookInfo;
import boyan.model.BookInfoCompareto;
import boyan.util.HttpClient;
import boyan.util.HttpClient.Charset;
import us.codecraft.webmagic.selector.Html;

/**
 * 
 * @author Administrator
 * 爬取豆瓣网图书资料
 * 
 * url:https://book.douban.com/tag/互联网?start=20&type=S
 * url:https://book.douban.com/tag/编程?start=20&type=S
 * url:https://book.douban.com/tag/算法?start=20&type=S
 * 
 */
public class DouBanCraw implements Runnable{
	
	public static Map<String,Boolean> flag = new HashMap<String, Boolean>();
	public static boolean printFlag = false;
	private List<String> url = new ArrayList<String>();
	private static List<BookInfo> bookinfos = new LinkedList<BookInfo>();
	
	private String key;
	private int getNum=0;
	private int urlNum=0;
	
	public DouBanCraw(String key) {
		this.key=key;
		addUrl();
	}
	
	private String getUrl(){
		if(url.size()<2){
			addUrl();
		}
		String thisUrl = url.get(0);
		url.remove(0);
		return thisUrl;
	}
	
	private void addUrl(){
		int max = urlNum+10;
		for(int i=urlNum;i<max;i++){
			int num = i*20;
			if(key.equals("hlw")){
				url.add("https://book.douban.com/tag/互联网?start="+num+"&type=S");
			}
			if(key.equals("bc")){
				url.add("https://book.douban.com/tag/编程?start="+num+"&type=S");
			}
			if(key.equals("sf")){
				url.add("https://book.douban.com/tag/算法?start="+num+"&type=S");
			}
			urlNum = i+1;
		}
	}
	
	
	public synchronized void save(BookInfo bookInfo){
		
		if(bookinfos.size()>100){
			bookinfos.add(bookInfo);
			Collections.sort(bookinfos, new BookInfoCompareto());
			//当大于100的时候，排序后删除100以后所有内容
			for(int i=100;i<bookinfos.size();i++){
				bookinfos.remove(i);
			}
		}else{
			bookinfos.add(bookInfo);
			Collections.sort(bookinfos, new BookInfoCompareto());
		}
			
	}
	
	public void run() {
		
		flag.put(key, false);
		while(true){
			
			String geturl = getUrl();
			List<BookInfo> handle = handle(geturl);
			getNum=getNum+handle.size();
			
			
			for (BookInfo bookInfo2 : handle) {
				save(bookInfo2);
			}
			
			if(handle.size()==0){
				System.out.println(key+"添加到true");
				flag.put(key, true);
			}
			
			if(flag.size()==3&&!flag.containsValue(false)){
				if(!printFlag){
						printf();
				}
				break;
			}
			
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
	}
	
	public synchronized void printf(){
		
			printFlag = true;
			Collections.sort(bookinfos, new BookInfoCompareto());
			for(int i=99;i<bookinfos.size();i++){
				bookinfos.remove(i);
			}
			System.out.println("打印"+bookinfos.size());
			Export.export(bookinfos);
	}
	
	
	private List<BookInfo> handle(String url){
		
		Map<String,String> Header = new HashMap<String, String>();
		Header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36");
		Header.put("Host", "book.douban.com");
		String text = HttpClient.HttpGetIn(url, Header, Charset.UTF_8);
		
		List<BookInfo> list = new ArrayList<BookInfo>();
		
		Html html = Html.create(text);
		
		List<String> all = html.xpath("//*[@id='subject_list']/ul/li/[@class='info']").all();
		
		StringBuilder titile = null;
		BookInfo bookInfo = null;
		
		for (String string : all) {
			
			Html li = Html.create(string);
			
			titile =  new StringBuilder();
			String string3 = li.xpath("//*[@class='info']/h2/a/text()").get();
			titile.append(string3.trim());
			List<String> all2 = li.xpath("//*[@class='info']/h2/a/span/text()").all();
			for (String string2 : all2) {
				titile.append(string2.trim());
			}
			
			String rating = li.regex("<span class=\"rating_nums\">(.+?)</span>").get();
			String pl = li.regex("<span class=\"pl\">(.+?)</span>").get().trim().replace("人评价)", "").replace("(", "");
			String pub = li.xpath("//*[@class='info']/div[@class='pub']/text()").get();
			String[] pubs = pub.split("/");
			
			if(StringUtils.isNumeric(pl)){
				Integer plInt = Integer.valueOf(pl);
				if(plInt<1000) continue;
			}else{
				continue;
			}
			
			
			bookInfo = new BookInfo();
			bookInfo.setBookName(titile.toString());
			bookInfo.setRating(rating);
			bookInfo.setRatingNums(pl);
			bookInfo.setAuthor(pubs[0]);
			
			for (String string2 : pubs) {
				if(string2.contains("-")){
					bookInfo.setDatePress(string2.trim());
				}
				if(string2.contains(".")){
					bookInfo.setMoney(string2.trim());
				}
				if(string2.contains("出版社")){
					bookInfo.setPress(string2.trim());
				}
			}
			
			list.add(bookInfo);
		}
		
		return list;
	}
	
}
